﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Srvtools
{
    public partial class UsasComboBoxTextForm : Form
    {
        string m_FileName = "";
        System.Drawing.Rectangle m_Rec = System.Drawing.Rectangle.Empty;

        public UsasComboBoxTextForm(string fileName, Rectangle rec)
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.FormClosing += new FormClosingEventHandler(ComboBoxTextForm_FormClosing);            
            this.txt.KeyDown += new KeyEventHandler(txt_KeyDown);
            this.Deactivate += new EventHandler(ComboBoxTextForm_Deactivate);
            m_FileName = fileName;
            m_Rec = rec;
            LoadData();
            this.Left = m_Rec.Left + m_Rec.Width + this.Width< Screen.PrimaryScreen.WorkingArea.Width ?
                m_Rec.Left + m_Rec.Width : m_Rec.Left - this.Width;
            this.Top = m_Rec.Top - this.Height > 0 ? m_Rec.Top - this.Height : m_Rec.Top+m_Rec.Height;
        }

        void ComboBoxTextForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Owner != null)
            {
                this.Owner.Focus();
            }
        }

        void ComboBoxTextForm_Deactivate(object sender, EventArgs e)
        {
            SaveData();
            this.Hide();
            this.Close();
            this.Dispose();
        }

        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control && (e.KeyCode == Keys.S || e.KeyCode == Keys.Enter)) || e.KeyCode == Keys.Escape)
            {                
                this.Close();
            }
            if (e.Control && e.KeyCode == Keys.A)
            {                
                this.txt.Select(0, this.txt.Text.Length);
            }
        }

        void LoadData()
        {
            if (System.IO.File.Exists(m_FileName))
            {
                System.IO.StreamReader sr = new StreamReader(m_FileName);
                string str = "";
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Trim() != "")
                    {
                        this.txt.Text += str + "\r\n";
                    }
                }
                sr.Close();
            }
            this.txt.Focus();
            this.txt.Select(this.txt.Text.Length, 0);
            this.txt.ScrollToCaret();
        }

        void SaveData()
        {
            FileStream fs = new FileStream(m_FileName, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            foreach (string s in this.txt.Lines)
            {
                sw.WriteLine(s);
            }            
            sw.Close();            
            fs.Close();
        }
    }
}

